//
//  StatusOringnalView.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/12.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//定义全局的间距
let StatusCellMargin: CGFloat = 10

class StatusOringnalView: UIView {
    
    //声明约束属性 对象类型是Constraint
    var bottomConstranit: Constraint?
    
    
    var status: Status? {
        didSet {
            //绑定模型数据
            iconView.sd_setImageWithURL(status?.user?.headImageURL)
            nameLabel.text = status?.user?.name
            mbrank_image.image = status?.user?.mbrank_image
            verified_image.image = status?.user?.verfied_type_image
            
            //TODO: 后期待完善-> OC中可以使用警告来标示待做事项
            contentLabel.text = status?.text
//            sourceLabel.text = status?.source
//            timeLabel.text = status?.created_at
            
            //在动态更新约束之前 卸载约束 在cell复用下 约束也被复用
            bottomConstranit?.uninstall()
            
            //给配图视图数据源赋值
            //原创微博没有配图视图 数组不能为空
//            print(status?.imageURLs?.count)
            
            if let imgURLs = status?.imageURLs where imgURLs.count != 0 {
//                print("有配图视图")
                
                //显示配图视图
                pictureView.hidden = false
                pictureView.imageURLs = imgURLs
//                pictureView.imageURLs = status?.imageURLs
                
                //修改原创微博底部约束 相对于配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstranit = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            } else {
//                print("-----没有配图视图----")
                
                //隐藏配图视图
                pictureView.hidden = true
                
                pictureView.imageURLs = nil
                //相对于微博正文
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstranit = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI
    private func setupUI() {
    
        backgroundColor = UIColor.whiteColor()
        //添加子视图
        addSubview(lineView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbrank_image)
        addSubview(verified_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        lineView.backgroundColor = UIColor.redColor()
        lineView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(lineView.snp_top).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 36, height: 36))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            
        }
        
        verified_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
            //搞定数的文本  make.right.equalTo(iconView.snp_right).offset(-StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        
        //不能够满足变化
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            //直接设置宽和高 不需要设置右边约束 测试宽度和高度
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
      
        //将用户头像处理成圆角
        iconView.layer.cornerRadius = 18
        iconView.layer.masksToBounds = true
        
        //添加原创微博的底部约束
        self.snp_makeConstraints { (make) -> Void in
            //保存底部约束对象 并且设置底部约束
           self.bottomConstranit = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
   
    }
    
    //懒加载 微博数据
    private lazy var lineView: UIView = UIView()
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var mbrank_image: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verified_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var nameLabel: UILabel = UIFactory.labelFactory("25期", textColor: UIColor.darkGrayColor(), fontSize: 15)
    private lazy var timeLabel: UILabel = UIFactory.labelFactory("25:25", textColor: UIColor.darkGrayColor(), fontSize: 11)
    private lazy var sourceLabel: UILabel = UIFactory.labelFactory("25期的老王八", textColor: UIColor.darkGrayColor(), fontSize: 11)
    private lazy var contentLabel: UILabel = UIFactory.labelFactory("25你若安好, 我必枪炮", textColor: UIColor.darkGrayColor(), fontSize: 14,alignment: .Left)
    //添加配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
    
}
